package primalgorithms.other;

public class _03ReverseBits {
    // you need treat n as an unsigned value
    public static int reverseBits(int n) {
        StringBuilder s = new StringBuilder(Integer.toBinaryString(n));
        if (n <= 0){
            return Integer.parseUnsignedInt(s.reverse().toString(),2);
        }
        while (s.length() < 32){
            s.insert(0,'0');
        }
        return Integer.parseUnsignedInt(s.reverse().toString(),2);
    }
}
